package action;

import controller.Action;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Curso;
import model.Disciplina;
import model.Professor;
import model.Turma;


public abstract class ManterTurmaAction extends Action{

    @Override
    public void prepararOperacao(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        try {
            String action = request.getParameter("action");
            request.setAttribute("action", action);
            setParametros(request, response);
            request.setAttribute("cursos", Curso.obterCursos());
            request.setAttribute("professores", Professor.obterProfessores());
            request.setAttribute("disciplinas", Disciplina.obterDisciplinas());
            RequestDispatcher view = request.getRequestDispatcher("/manterTurma.jsp");
            view.forward(request, response);
        } catch (ServletException e) {
            throw e;
        } catch (IOException e) {
            throw new ServletException(e);
        } catch (SQLException e) {
            throw new ServletException(e);
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        }
    }

    @Override
    public void confirmarOperacao(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        try {
            int codTurma = Integer.parseInt(request.getParameter("txtCodTurma"));
            int codDisciplina = Integer.parseInt(request.getParameter("optDisciplina"));
            int numVagas = Integer.parseInt(request.getParameter("txtVagas"));
            int matricula = Integer.parseInt(request.getParameter("optProfessor"));
            String numSala = request.getParameter("txtSala");
            int ano = Integer.parseInt(request.getParameter("txtAno"));
            int semestre = Integer.parseInt(request.getParameter("optSemestre"));
            Disciplina disciplina = null;
            if (codDisciplina != 0) {
                disciplina = Disciplina.obterDisciplina(codDisciplina);
            }
            Professor professor = null;
            if (matricula != 0) {
                professor = Professor.obterProfessor(matricula);
            }
            Turma turma = new Turma(codTurma, professor, disciplina, semestre, ano, numVagas, numSala);
            efetuaPersistencia(turma);
            RequestDispatcher view = request.getRequestDispatcher("FrontController?action=PesquisarTurma");
            view.forward(request, response);
        } catch (ServletException e) {
            throw e;
        } catch (IOException e) {
            throw new ServletException(e);
        } catch (SQLException e) {
            throw new ServletException(e);
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        }
    }
    
    public abstract void setParametros(HttpServletRequest request, HttpServletResponse response) throws ServletException;

    public abstract void efetuaPersistencia(Turma turma) throws ServletException;
}
